package com.example.fwcz.handler;


import com.example.fwcz.exception.NotFoundException;
import com.example.fwcz.exception.PersistenceException;
import com.example.fwcz.model.vo.Result;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class ControllerExceptionHandler {
    @ExceptionHandler(NotFoundException.class)
    public Result notFoundExceptionHandler(HttpServletRequest request,
                                           NotFoundException e){
        return Result.create(404,e.getMessage());
    }

    @ExceptionHandler(PersistenceException.class)
    public Result persistenceExceptionHandler(HttpServletRequest request,
                                              PersistenceException e){
        return Result.create(500,e.getMessage());
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public Result usernameNotFoundExceptionHandler(HttpServletRequest request,
                                                   UsernameNotFoundException e){
        return Result.create(401,e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(HttpServletRequest request,
                                   Exception e){
        // --- exception e 暂不做处理
        return Result.create(500,"异常错误");
    }
}
